package com.bdqn.gulimall.order.web;

import com.alipay.api.AlipayApiException;
import com.bdqn.gulimall.order.config.AlipayTemplate;
import com.bdqn.gulimall.order.service.OrderService;
import com.bdqn.gulimall.order.vo.PayVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * PayWebController
 *
 * @author f
 * @since 2022/3/9
 */
@Controller
public class PayWebController {

    @Resource
    AlipayTemplate alipayTemplate;

    @Resource
    OrderService orderService;

    /**
     *
     * 1.将支付页让浏览器展示
     * 2.支付成功后，要跳到用户的订单列表页
     * @param orderSn 订单号
     * @return
     * @throws AlipayApiException
     */
    @ResponseBody                       //返回一个收银页面（html）
    @GetMapping(value = "/aliPayOrder",produces = "text/html")      //抛出支付方法的异常
    public String payOrder(@RequestParam("orderSn") String orderSn) throws AlipayApiException {

        // PayVo payVo = new PayVo();
        // payVo.setBody();//订单的备注
        // payVo.setOut_trade_no();//订单号（对外交易号）
        // payVo.setSubject();//订单的主（标）题（订单名称）
        // payVo.setTotal_amount();//订单的金额
        /**
         * 获取当前订单的支付信息
         */
        PayVo payVo = orderService.getOrderPay(orderSn);
        //支付方法，返回收银页面，将此页面直接交给浏览器
        String pay = alipayTemplate.pay(payVo);
        System.out.println(pay);

        return pay;
    }
}
